package com.tang.lease.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.lease.common.constant.RedisConstant;
import com.tang.lease.common.login.LoginUserHolder;
import com.tang.lease.model.entity.*;
import com.tang.lease.model.enums.ItemType;
import com.tang.lease.web.app.mapper.*;
import com.tang.lease.web.app.service.ApartmentInfoService;
import com.tang.lease.web.app.service.BrowsingHistoryService;
import com.tang.lease.web.app.service.RoomInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.lease.web.app.vo.apartment.ApartmentItemVo;
import com.tang.lease.web.app.vo.attr.AttrValueVo;
import com.tang.lease.web.app.vo.fee.FeeValueVo;
import com.tang.lease.web.app.vo.graph.GraphVo;
import com.tang.lease.web.app.vo.room.RoomDetailVo;
import com.tang.lease.web.app.vo.room.RoomItemVo;
import com.tang.lease.web.app.vo.room.RoomQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomInfo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        String key = RedisConstant.APP_LOGIN_PREFIX + id;
        RoomDetailVo roomDetailVo = (RoomDetailVo) redisTemplate.opsForValue().get(key);
        //查询缓存，没有缓存，将查询的结果存入缓存
        if (roomDetailVo==null) {
            //查询房间信息
            RoomInfo roomInfo = roomInfoMapper.selectById(id);
            if (roomInfo == null) {
                return null;
            }
            //查询图片信息
            List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
            //查询属性信息
            List<AttrValueVo> attrValueVoList = roomAttrValueMapper.selectListByRomId(id);
            //查询配套信息
            List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectListByRoomId(id);
            //查询标签信息
            List<LabelInfo> labelInfoList = roomLabelMapper.selectListByRoomId(id);
            //查询支付方式信息
            List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectListByRoomId(id);
            //查询杂费信息
            List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.selectListByApartmentId(roomInfo.getApartmentId());
            //查询租期信息
            List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectListByRoomId(id);
            //查询公寓信息
            ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentVoById(roomInfo.getApartmentId());
            roomDetailVo = new RoomDetailVo();
            BeanUtils.copyProperties(roomInfo, roomDetailVo);
            roomDetailVo.setGraphVoList(graphVoList);
            roomDetailVo.setAttrValueVoList(attrValueVoList);
            roomDetailVo.setFacilityInfoList(facilityInfoList);
            roomDetailVo.setLabelInfoList(labelInfoList);
            roomDetailVo.setPaymentTypeList(paymentTypeList);
            roomDetailVo.setFeeValueVoList(feeValueVoList);
            roomDetailVo.setLeaseTermList(leaseTermList);
            roomDetailVo.setApartmentItemVo(apartmentItemVo);
            redisTemplate.opsForValue().set(key,roomDetailVo);
        }
        browsingHistoryService.saveHistory(LoginUserHolder.getLoginUser().getUserId(),id);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<RoomInfo> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page,id);
    }
}




